package com.y2.authprj.mvc.controller.auth;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdglc.spring.biz.auth.FunctionauthorityBiz;
import com.gdglc.spring.biz.auth.RoleBiz;
import com.gdglc.spring.biz.auth.RoleauthorityBiz;
import com.gdglc.spring.biz.auth.SysroleBiz;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.FunctionauthorityInfo;
import com.y2.authprj.bean.auth.model.RoleInfo;
import com.y2.authprj.bean.auth.model.RoleauthorityInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.exception.BizException;

@Controller
@RequestMapping("/auth/role")
public class RoleController {


	@Resource(name="SysroleBiz")
	private SysroleBiz sysbiz;
	@Resource(name="FunctionauthorityBiz")
	private FunctionauthorityBiz functionbiz;
	
	@Resource(name="RoleBiz")
	private RoleBiz roleBiz;

	public RoleBiz getRoleBiz() {
		return roleBiz;
	}

	public void setRoleBiz(RoleBiz roleBiz) {
		this.roleBiz = roleBiz;
	}
	
	public SysroleBiz getSysbiz() {
		return sysbiz;
	}

	public void setSysbiz(SysroleBiz sysbiz) {
		this.sysbiz = sysbiz;
	}

	public FunctionauthorityBiz getFunctionbiz() {
		return functionbiz;
	}

	public void setFunctionbiz(FunctionauthorityBiz functionbiz) {
		this.functionbiz = functionbiz;
	}

	@RequestMapping("/page")
	public String page(Model model,@RequestParam(
			required=false,defaultValue = "1")int pageNum,
			@RequestParam(required=false,defaultValue = "")String name){
		PageInfo<RoleInfo> queryByPage = roleBiz.findAllByName(name, pageNum, 5);
		
		List<RoleInfo> roleInfos = queryByPage.getList();
		
		System.out.println(queryByPage);
		System.out.println(roleInfos);
		model.addAttribute("queryByPage",queryByPage);
		model.addAttribute("roleInfos",roleInfos);
		model.addAttribute("names",name);
		return "/auth/role/roleList";
	}
	
	@RequestMapping("showadd")
	public String showadd(String[] delitems,Model model) {
		List<SysroleInfo> list=new ArrayList<>();
		for (int i = 0; i < delitems.length; i++) {
			try {
				List<RoleInfo> roleInfolist = roleBiz.findByRoleid(Integer.parseInt(delitems[i]));
				model.addAttribute("roleInfolist", roleInfolist);
				SysroleInfo sysrole=sysbiz.findByid(Integer.parseInt(delitems[i]));
				list.add(sysrole);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			List<FunctionauthorityInfo> functionlist=functionbiz.findAll();
			model.addAttribute("functionlist", functionlist);
		} catch (BizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("sysrolelist", list);

		return "/auth/role/roleauth";
	}
	@RequestMapping("add")
	public String add(String[] rList,String[] fList, Model model) {
		List<RoleInfo> roleList=null;
		RoleInfo rolea=new RoleInfo();
			for (int i = 0; i < rList.length; i++) {
				rolea.setRoleId(Integer.parseInt(rList[i]));
				try {
					 roleList=roleBiz.findByRoleid(Integer.parseInt(rList[i]));
				} catch (BizException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				for (int j = 0; j < fList.length; j++) {
					Boolean fage=true;
					rolea.setFunctionauthorityId(Integer.parseInt(fList[j]));
					for (RoleInfo roleInfo : roleList) {
						if(roleInfo.getFunctionauthorityId()==Integer.parseInt(fList[j])) {
							fage=false;
							break;
						}else {
							fage=true;
						}
					}
//					for (RoleauthorityInfo roleauthorityInfo : roleList) {
//						if(roleauthorityInfo.getFunctionauthorityId()==rolea.getFunctionid()[j]) {
//							fage=false;
//						}
//					}
					if(fage) {	
						try {
							roleBiz.add(rolea);
						} catch (BizException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

					
				}
				
			}
				
		return "redirect:/auth/sysrole/page";
	}
	
	@RequestMapping("/findByRoleid")
	public String findByRoleid(Integer roleId,Model model){
		try {
			List<RoleInfo> roleInfo = roleBiz.findByRoleid(roleId);
			model.addAttribute("roleInfo", roleInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "auth/role/roleFunc";
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String[] delitems,Model model){
		for (int i = 0; i < delitems.length; i++) {
			try {
				roleBiz.deleteRole(Integer.parseInt(delitems[i]));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return "";
	}
	
}
